package com.swh.xbsc.controller;


import com.baomidou.kaptcha.Kaptcha;
import com.swh.xbsc.entity.Result;
import com.swh.xbsc.entity.User;
import com.swh.xbsc.service.UserService;
import com.swh.xbsc.util.EmailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;


/**
 * @Author: swh
 * @Description:
 * @Date: 2020/10/23 18:43
 * @Version: 1.0
 */
@RequestMapping("main")
@Controller
public class MainController {

    @Autowired
    private Kaptcha kaptcha;

    @Autowired
    private UserService userService;

    @RequestMapping("sidebar")
    public String sidebar() {
        return "/common/sidebar.html";
    }


    @RequestMapping("index")
    @ResponseBody
    public Result index(String username, String password, String code, HttpSession session) {
        if (kaptcha.validate(code)) {
            User login = new User();
            login.setUsername(username);
            login.setPassword(password);
            login = userService.selectOne(login);
            //判断用户是否为空如果存在则不为null
            if (login != null) {
                //正确登录用户
                //存session
                session.setAttribute("login", login);
                //清空密码
                login.setPassword(null);


                return new Result(true, "登录成功", login);
            } else {
                return new Result(false, "用户名或密码错误", null);
            }
        }
        return new Result(false, "验证码错误", null);
    }

    @RequestMapping("sendEmail")
    @ResponseBody
    public Result sendEmail(HttpServletRequest request, String username, HttpSession session) {
        if (username != null && username != "") {
            try {
                //纯数字的验证码
                int result = (int) ((Math.random() + 1) * 1000);
                String code = String.valueOf(result);

                //包含数字和英文的验证码
                String ZiMu = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGJKLZXCVBNM1234567890";
//            String code = "";
//            Random random = new Random();
//            for (int i = 0; i < 4; i++) {
//                int index = random.nextInt(ZiMu.length());
//                char c = ZiMu.charAt(index);
//                code += c;
//            }


                //存session
                session.setAttribute("code", code);
                session.setMaxInactiveInterval(60);
                System.out.println(session.getAttribute(code));
                EmailUtil.sendEmail(username, code);
                System.out.println("======================");
                System.out.println(code);

                return new Result(true, "发送成功", code);

            } catch (Exception e) {

                e.printStackTrace();

                return new Result(false, "发送失败", null);
            }
        }
        return new Result(false, "邮箱账号错误", null);
    }

    //注册
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    @ResponseBody
    public Result insert(@RequestBody User user) {
        user.setRegisterTime(new Date());
        user.setInfo("我是谁");
        user.setPhone("132456");
        user.setDeptName("研发部");
        user.setAge(20);
        user.setLook(30);
        user.setIsSecret("0");

        return new Result(userService.insertSelective(user));
    }


}
